#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Módulos 
import sys, gtk, pygame, utils, objetos
from pygame.locals import *

# Constantes

# Clases
# ---------------------------------------------------------------------
        
class Linea:
	def __init__(self,texto,posx,posy):
		self.text,self.rect = utils.textotls(texto, posx,posy,16,(0,0,0))


class Linea2:
	def __init__(self,texto,posx,posy):
		self.text,self.rect = utils.textotlb(texto, posx,posy,20,(50,50,255))

class LineaA:
	def __init__(self,texto,posx,posy):
		self.text,self.rect = utils.textotls(texto, posx,posy,22,(255,0,0))		

class Textbox:
	def __init__(self,posx,posy,match,maxlen):
		self.font = pygame.font.Font("fonts/TRSL____.ttf", int(18*utils.RES))
		self.rect = pygame.Rect([0, 0, 110*utils.RES, 22*utils.RES])
		offset = (3, 3)
		self.rect.centerx = posx
		self.rect.centery = posy
		self.rect.left += offset[0]
		self.rect.top  += offset[1]
		self.message = ""
		self.selected = False
		self.puntos,self.puntos_rect = utils.texto_normal('................', posx-20*utils.RES,posy+10*utils.RES)
		self.maxlen = maxlen
		self.sonido = pygame.mixer.Sound("sounds/letra.ogg")
		self.check_img = utils.load_image("images/check.png",True)
		self.check_rect = self.check_img.get_rect()
		self.check_rect.centerx = posx+50*utils.RES
		self.check_rect.centery = posy
		self.match = match
	
	def display(self,screen):
		screen.blit(self.puntos,self.puntos_rect)
		if len(self.message) != 0:
			#self.message = self.message.encode("iso-8859-1")
			screen.blit(self.font.render(self.message, 1, (0,0,0)), self.rect.topleft)	
			if self.message == self.match:
				screen.blit(self.check_img,self.check_rect)
   	

# ---------------------------------------------------------------------
def dibujar(screen,cursor_picture, clock):
    salir = 0
    background_image = utils.load_image('images/fondo_3.jpg')
    i= 0
    j= 0
    numero_linea = 1
    btn_adelante = objetos.BotonAdelante()
    btn_atras = objetos.BotonAtras()
    
    
    clase, clase_rect = utils.texto_clase("Clase 3", 740*utils.RES, 30*utils.RES)
           
    titulo, titulo_rect = utils.texto_subtitulo_azul("MINIMIZAR LA GENERACIÓN DE BASURA", 400*utils.RES, 60*utils.RES)
     
    bloque1 = ( Linea('Podemos   EDUCIR la cantidad de basura si aprovechamos bien lo que consumimos. Otra forma', 368*utils.RES,90*utils.RES),
    Linea('de evitar generar tanta basura es   EUTILIZAR los materiales cuando sea posible: algunos objetos', 370*utils.RES,110*utils.RES),
    Linea('o envases podemos usarlos para otra cosa en lugar de desecharlos.', 280*utils.RES,130*utils.RES),
    Linea2('Escribí junto a cada cartel si se trata de una', 520*utils.RES,170*utils.RES),
    Linea2('acción para REDUCIR o REUTILIZAR la basura.', 523*utils.RES,195*utils.RES),
    LineaA('R', 133*utils.RES,89*utils.RES),
    LineaA('R', 281*utils.RES,109*utils.RES)
    )    
    
    img1 = utils.load_image('images/c3_9_1.jpg')
    
    textboxes = (Textbox(400*utils.RES,240*utils.RES,"reducir",7),Textbox(630*utils.RES,390*utils.RES,"reutilizar",10))
    
    while salir == 0:
    
    	# Pump GTK messages.sub
		while gtk.events_pending():
			gtk.main_iteration()
			
		for eventos in pygame.event.get():
			if eventos.type == QUIT:
				sys.exit(0)
			elif eventos.type == pygame.VIDEORESIZE:
			   pygame.display.set_mode(eventos.size, pygame.RESIZABLE)	
			elif eventos.type == MOUSEBUTTONDOWN:
				if btn_adelante.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 10
					btn_adelante.sonido.play()	
				elif btn_atras.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 8					
					btn_atras.sonido.play()	
				for textbox in textboxes:
					if textbox.rect.collidepoint(pygame.mouse.get_pos()):
						textbox.selected = True
						textbox.sonido.play()
					else:
						textbox.selected = False
			elif eventos.type == KEYDOWN:		
				if eventos.key == K_BACKSPACE:
					for textbox in textboxes:
						if textbox.selected == True: 
							textbox.message = textbox.message[0:-1]
				elif eventos.key == K_RETURN:
					break
				else:
					for textbox in textboxes:
						if textbox.selected == True:
							textbox.sonido.play() 
							if  len(textbox.message) <= textbox.maxlen:
								textbox.message += eventos.unicode.encode("iso-8859-1")		
						

		# Realiza la animacion del texto
		screen.blit(background_image, (0, 0))
		screen.blit(img1, (32*utils.RES, 132*utils.RES))
		screen.blit(clase, clase_rect)
		screen.blit(titulo, titulo_rect)
		for palabra in bloque1:
			screen.blit(palabra.text, palabra.rect)
		for textbox in textboxes:
			textbox.display(screen)
		screen.blit(btn_adelante.image, btn_adelante.rect)
		screen.blit(btn_atras.image, btn_atras.rect)
		screen.blit(cursor_picture, pygame.mouse.get_pos())
		pygame.display.flip()

		# Try to stay at 30 FPS
		clock.tick(30)
                	 
    return salir

# This function is called when the game is run directly from the command line:
# ./TestGame.py 
def main():
	
	pygame.init()
	screen = pygame.display.set_mode((int(800*utils.RES), int(520*utils.RES)), pygame.RESIZABLE) #pygame.display.get_surface() #

	# Fake a Sugar cursor for the pyGame canvas area
	pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
	cursor_picture = pygame.image.load('images/standardcursor.png')
	cursor_picture.convert_alpha() 
	clock = pygame.time.Clock()
	dibujar(screen,cursor_picture,clock)

if __name__ == '__main__':
   main()
